package com.siki.controller;

import com.siki.config.filterConfig.jwtConfig.annotation.PassToken;
import com.siki.config.filterConfig.jwtConfig.annotation.UserLoginToken;
import com.siki.dto.user.UserInfoSaveDTO;
import com.siki.dto.user.UserPasswordDTO;
import com.siki.dto.user.UserResumeInfoSaveDTO;
import com.siki.dto.user.UserSignUpDTO;
import com.siki.service.UserService;
import com.siki.utils.JsonInfoReturn;
import com.siki.vo.user.GetPositionListVO;
import com.siki.vo.user.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.lang.NonNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.validation.constraints.NotNull;

/**
 * @Author Siki
 * @Date 2020/6/6
 */
@RestController
@Api(tags = "个人用户相关操作模块")
@RequestMapping("/api/vi/siki/user")
@CrossOrigin
@UserLoginToken
public class UserController {

    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/sign_up",method = RequestMethod.POST)
    @PassToken
    @ApiOperation(value = "[注册接口] - [企业/个人]用户注册",consumes = "application/json", httpMethod = "POST")
    public JsonInfoReturn signUp(@RequestBody @Validated @ApiParam("用户注册DTO") UserSignUpDTO dto){
        userService.signUp(dto);
        return JsonInfoReturn.success();
    }

    @RequestMapping(value = "/save_user_info",method = RequestMethod.POST)
    @ApiOperation(value = "[提交接口] - 用户信息保存",consumes = "application/json",
            httpMethod = "POST")
    public JsonInfoReturn saveUserInfo(@RequestBody @Validated @ApiParam("用户信息提交DTO")UserInfoSaveDTO dto,
                                       Authentication authentication){
        userService.saveUserInfo(dto,authentication.getName());
        return JsonInfoReturn.success();
    }

    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ApiOperation(value = "[查询接口] - 获取当前用户信息",consumes = "application/json",
            httpMethod = "GET",response = UserInfoVO.class)
    public JsonInfoReturn get(Authentication authentication){
        return JsonInfoReturn.success(new UserInfoVO(userService.getUser(authentication.getName())));
    }

    @RequestMapping(value = "/update_password",method = RequestMethod.POST)
    @ApiOperation(value = "[提交接口] - 用户修改密码",consumes = "application/json",httpMethod = "POST")
    public JsonInfoReturn updatePassword(@RequestBody @Validated @ApiParam("修改密码DTO")UserPasswordDTO dto,
                                         Authentication authentication){
        return userService.savePassword(dto,authentication.getName());
    }

    @ApiOperation(value = "[查询接口] - 当前用户查询已应聘的岗位",consumes = "application/json",httpMethod = "GET")
    @RequestMapping(value = "/get_position",method = RequestMethod.POST)
    public JsonInfoReturn getPosition(Authentication authentication){
        return JsonInfoReturn.success(userService.getPosition(authentication.getName())
                .stream().map(GetPositionListVO::new));
    }
}
